package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.ProfitAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.ProfitInfo;
import com.haotunet.app.goldtreasure.model.ProfitModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;

import java.util.List;

public class MyProfitActivity extends BaseActivity {
	private ListView mlvProfit;
	private TextView mTotalIncome;
	private final int GET_MYPROFIT_SUCCESS = 2;
	private Handler handle = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case GET_MYPROFIT_SUCCESS:
				ProfitModel model = (ProfitModel) msg.obj;
				mTotalIncome.setText(model.getTotalIncome());
				List<ProfitInfo> profitInfo = model.getIncomeRecordVMList();
				mlvProfit.setAdapter(new ProfitAdapter(MyProfitActivity.this, profitInfo));
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_profit);
		initView();
		initData();
	}

	private void initData() {
		ActionUtil actionUtil = new ActionUtil(MyProfitActivity.this);
		actionUtil.getMyProfit();
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (isFinishing()){
					return;
				}
				ProfitModel profitModel = (ProfitModel) model;
				if (profitModel == null) {
					UIHelper.showToast(MyProfitActivity.this, getString(R.string.rank_fail));
					return;
				}
				if (profitModel.getErrorMsg() != null) {
					if (profitModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
						ActivityUtils.forceQuit(MyProfitActivity.this, getString(R.string.crowding_in_tips_txt));
					} else {
						UIHelper.showToast(MyProfitActivity.this, profitModel.getErrorMsg());
					}
				} else {
					Message msg = new Message();
					msg.what = GET_MYPROFIT_SUCCESS;
					msg.obj = profitModel;
					handle.sendMessage(msg);
				}
			}
		});
	}

	private void initView() {

		TitleView mTitleView = (TitleView) findViewById(R.id.title_view);
		mTitleView.getTitletextView().setVisibility(View.VISIBLE);
		mTitleView.getTitletextView().setText(getString(R.string.cumulative_gain));
		mTitleView.getBackView().setVisibility(View.VISIBLE);
		mTitleView.getBackView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		TextView mIncomeProduct = (TextView) findViewById(R.id.income_products);
		mIncomeProduct.setText(getString(R.string.current_gold_g));
		mTotalIncome = (TextView) findViewById(R.id.total_income);
		mlvProfit = (ListView) findViewById(R.id.lv_profit);

	}

}
